package org.example.back.controller.Forum;


import org.example.back.entity.Forum.ForumPost;
import org.example.back.service.Forum.ForumPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/forum/posts")
public class ForumPostController {

    @Autowired
    private ForumPostService forumPostService;

    // 发布新帖
    @PostMapping
    public String createPost(@RequestBody ForumPost post) {
        forumPostService.createPost(post);
        return "发布成功";
    }

    // 查看所有帖子
    @GetMapping
    public List<ForumPost> getAllPosts() {
        return forumPostService.getAllPosts();
    }

    // 查看某个帖子，并自动加1浏览量
    @GetMapping("/{id}")
    public ForumPost getPostById(@PathVariable Long id) {
        forumPostService.increaseViews(id);
        return forumPostService.getPostById(id);
    }

    // 删除帖子
    @DeleteMapping("/{id}")
    public String deletePost(@PathVariable Long id) {
        forumPostService.deletePost(id);
        return "删除成功";
    }
}

